package view;


import java.sql.SQLException;
import java.util.ArrayList;

import com.example.pruebapfi.R;













import controller.RegimenAlimenticioController;
import DB.DatabaseHelper;
import android.support.v4.app.Fragment;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.PorterDuff.Mode;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;


public class MainActivityButtonsFragment extends Fragment {
	
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.activity_main_activity_buttons, container, false);
        final Activity activity=getActivity();
	     try {
	         /*Si no existe un Regimen Alimenticio en la base de datos, lo redirecciona para crear uno*/
	 	        DatabaseHelper db= new DatabaseHelper(activity.getApplicationContext());
	 			if(db.getRegimenAlimenticioDao().queryBuilder().queryForFirst() == null){
	 				//Muestro las condiciones de uso
	 				
	 				
	 				final Dialog mDialog = new Dialog(v.getContext());
	 				mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
	 				mDialog.setContentView(R.layout.overlay_view_condiciones);
	 				Drawable d = new ColorDrawable(R.color.gray_background);
	 				d.setAlpha(130);
	 				mDialog.getWindow().setBackgroundDrawable(d);
	 				mDialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.MATCH_PARENT);
	 				mDialog.show();
	 				
	 				Button aceptar = (Button) mDialog.findViewById(R.id.buttonAcepto);
	 				Button rechazar = (Button) mDialog.findViewById(R.id.ButtonRechazo);
	 				
	 				aceptar.setOnClickListener(new OnClickListener() {
	 					
	 					public void onClick(View arg0) {
	 						
	 						mDialog.dismiss();
	 						Intent intent= new Intent(activity.getApplicationContext(),IngresarValoresDieta.class);
	 		 				startActivity(intent);
	 					}
	 					
	 				});
	 				
	 				rechazar.setOnClickListener(new OnClickListener() {
	 					
	 					public void onClick(View arg0) {
	 						System.exit(0);
	 					}
	 					
	 				});
	 			}
	 		} catch (SQLException e) {
	 			e.printStackTrace();
	 		}
	     	 refrescarValoresLiquido(v,activity);	
	         refrescarValoresNutricionales(v,activity);
	         Button button_eat= (Button) v.findViewById( R.id.ButtonEat);
	         Button button_drink= (Button) v.findViewById( R.id.ButtonDrink);
	         Button button_edit_nutr= (Button) v.findViewById( R.id.ButtonEditNutr);
	         Button button_idea= (Button) v.findViewById( R.id.ButtonIdea);
	         Button button_notif= (Button) v.findViewById( R.id.ButtonNotif);
	         Button button_support= (Button) v.findViewById( R.id.ButtonSupport);
	         
	         button_support.setOnClickListener(new OnClickListener() {
	        	 public void onClick(View v) {
	        		 	final Dialog mDialog = new Dialog(v.getContext());
	        		 	mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
	        		 	mDialog.setContentView(R.layout.overlay_view_main_help);
	        		    Drawable d = new ColorDrawable(R.color.gray_background);
	        		    d.setAlpha(130);
	        		    mDialog.getWindow().setBackgroundDrawable(d);
	        		    mDialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.MATCH_PARENT);
	        		    mDialog.show();
	        		    
	        		    LinearLayout layout = (LinearLayout) mDialog.findViewById(R.id.overlayLayout);

	        			layout.setOnClickListener(new OnClickListener() {

	        				@Override

	        				public void onClick(View arg0) {

	        					mDialog.dismiss();

	        				}

	        			});
	        		    
	        	 }
	         });
	         
	         button_idea.setOnClickListener(new OnClickListener() {
	        	 public void onClick(View v) {
	        		 Intent intent= new Intent(activity.getApplicationContext(),ListarConsejos.class);
	        		 startActivity(intent);	
	        	 }
	         });
	         
	         button_notif.setOnClickListener(new OnClickListener() {
	        	 public void onClick(View v) {
	        		 Intent intent= new Intent(activity.getApplicationContext(),ListarNotificaciones.class);
	        		 startActivity(intent);	
	        	 }
	         });
	         
	         button_edit_nutr.setOnClickListener(new OnClickListener() {
	 			public void onClick(View v) {
	 				Intent intent= new Intent(activity.getApplicationContext(),IngresarValoresDieta.class);
	 				startActivity(intent);	
	 			}
	 		});
	         
	         
	         button_eat.setOnClickListener(new OnClickListener() {
	 			@Override
	 			public void onClick(View v) {
	 				DatabaseHelper db= new DatabaseHelper(activity.getApplicationContext());
	 				try {
	 					//Verifica que exista un Regimen Alimenticio creado o lo manda a crear uno
	 					if(db.getRegimenAlimenticioDao().queryBuilder().queryForFirst() == null){
	 						Intent intent= new Intent(activity.getApplicationContext(),IngresarValoresDieta.class);
	 						startActivity(intent);
	 					}else{
	 						Intent intent= new Intent(activity.getApplicationContext(),ListarAlimentos.class);
	 						intent.putExtra("tipo", "ingesta");
	 						startActivity(intent);
	 					}
	 				} catch (SQLException e) {
	 					e.printStackTrace();
	 				}	
	 			}
	 		});
	         
	         button_drink.setOnClickListener(new OnClickListener() {
		 			@Override
		 			public void onClick(View v) {
		 				DatabaseHelper db= new DatabaseHelper(activity.getApplicationContext());
		 				try {
		 					//Verifica que exista un Regimen Alimenticio creado o lo manda a crear uno
		 					if(db.getRegimenAlimenticioDao().queryBuilder().queryForFirst() == null){
		 						Intent intent= new Intent(activity.getApplicationContext(),IngresarValoresDieta.class);
		 						startActivity(intent);
		 					}else{
		 						Intent intent= new Intent(activity.getApplicationContext(),IngresarLiquido.class);
		 						startActivity(intent);
		 					}
		 				} catch (SQLException e) {
		 					e.printStackTrace();
		 				}	
		 			}
		 		});
        return v;
    }

    public static MainActivityButtonsFragment newInstance() {

    	MainActivityButtonsFragment f = new MainActivityButtonsFragment();
    	
        Bundle b = new Bundle();
        f.setArguments(b);
        return f;
    }
    
    public void onResume() {
        super.onResume(); 
       refrescarValoresNutricionales(this.getView(),getActivity());
       refrescarValoresLiquido(this.getView(),getActivity());
    }
 private void refrescarValoresNutricionales(View v,Activity activity) {
	 TextView proteinaMain=(TextView) v.findViewById(R.id.TextProteinaMain);
		TextView calcioMain=(TextView) v.findViewById(R.id.TextCalcioMain);
		TextView fosforoMain=(TextView) v.findViewById(R.id.TextFosforoMain);
		TextView potasioMain=(TextView) v.findViewById(R.id.TextPotasioMain);
		TextView sodioMain=(TextView) v.findViewById(R.id.TextSodioMain);
		
		ArrayList<String> nutrientes=(ArrayList<String>) RegimenAlimenticioController.getInstance().obtenerCantidadesQuePuedoConsumir(activity.getApplicationContext());
		if(!nutrientes.isEmpty()){
			proteinaMain.setText(nutrientes.get(2));
			calcioMain.setText(nutrientes.get(1));
			fosforoMain.setText(nutrientes.get(3));
			potasioMain.setText(nutrientes.get(0));
			sodioMain.setText(nutrientes.get(4));
		}
	
}
 private void refrescarValoresLiquido(View v,Activity activity) {
	 	ProgressBar liquidoBar=(ProgressBar) v.findViewById(R.id.progressBarLiquido);
	 	TextView liquidoConsumidoText=(TextView) v.findViewById(R.id.textLiquidoConsumido);
		ArrayList<Integer> liquidos=(ArrayList<Integer>) RegimenAlimenticioController.getInstance().obtenerCantidadesQuePuedoConsumirLiquido(activity.getApplicationContext());
		if(!liquidos.isEmpty()){
			liquidoBar.setMax(liquidos.get(0));
			liquidoBar.setProgress(liquidos.get(1));
			if(liquidos.get(0)<liquidos.get(1)){
				liquidoBar.getProgressDrawable().setColorFilter(Color.rgb(214, 40, 49),Mode.MULTIPLY );
			}
			liquidoConsumidoText.setText(" "+liquidos.get(1)+"/"+liquidos.get(0)+" ml");
		}
	
}

}
